#include "container.h"

Container::Container(int argc,char* argv[], int nx, int ny, int nz, Double h) 
            :NaSt(argc,argv), fm(nx,ny,nz,h), lset(nx,ny,nz,h), pset(nx,ny,nz,h), 
            grid(nx,ny,nz), gridCell(nx,ny,nz), init(nx,ny,nz), dt(DT), Nx(nx), Ny(ny), Nz(nz) 
{
   SurfaceInitial(init, h);
   Clear(); 
}

void Container::Update()//chamda no loop (evento) IdleFunc da glut
{ 
   count++;
   cout << "\n\n iteracao: " << count<<"\n";
   for(int i=0;i<=NX+1;i++)
      for(int j=0;j<=NY+1;j++)
         for(int k=0;k<=NZ+1;k++)     
            NaSt.Phi[i][j][k] = lset.gridPhi(i,j,k);        
   //A velocidade no interior da superf�cie esta sendo atualizada aqui   
   //cout<<"\n Vou chamar a DoIt\n";
   NaSt.DoIt(gridCell);  
   //A velocidade na superf�cie e no exterior a superf�cie est� sendo atualiza aqui 
   for(int i=0;i<=NX+1;i++)
      for(int j=0;j<=NY+1;j++)
         for(int k=0;k<=NZ+1;k++)     
            {
               grid.gridU(i,j,k)  = NaSt.U[i][j][k];
               grid.gridV(i,j,k)  = NaSt.V[i][j][k];
               grid.gridW(i,j,k)  = NaSt.W[i][j][k];               
               fm.flagCell[i][j][k] = NaSt.flag[i][j][k];               
            }             
            
   //cout<<"\n Vou chamar a ReInitialize \n";
   lset.ReInitialize(fm, grid, gridCell); //Ajustando a Phi atrav�s do Fast Marching  
   //cout<<"/n Voltando da Reinitialize \n";
               
   double ddtt = NaSt.S.delt ;
   //double ddtt = 1.0 ;
   
   lset.Update(gridCell,ddtt);
   //cout<<"/n Voltando da Level_Set Update \n";  
   //pset.Update(gridCell,ddtt);
   //lset.Fix(pset);   
   //lset.Fix(pset);
}

void Container::Clear()
{
   lset.Initialize(init);//Faz gridPhi(i,j,k)=init(i,j,k)
   pset.Reseed(lset);//Deletes all particles and creates new ones. Only use this function when absolutely necessary
   count = 0;
}      

void Container::SurfaceInitial(Grid &init, Double h)
{//Observe que quando alteramos init aqui, precisamos alterar Phi em Navier::DefinirCenaInicial
   FOR_ALL_LS //0 ... N+1
   {
      float ic = i+0.5; float jc = j+0.5; float kc = k+0.5;
      double signdisthor = sqrt((jc-16)*(jc-16) + (kc-16)*(kc-16))-5.0;
      double  disthor= fmax(0,signdisthor);            
      if(i>0) init(i,j,k)  = 1.0/NX * (sqrt(disthor*disthor+(ic-1.0)*(ic-1.0)))-1E-5 ; 
      else init(i,j,k) = 1.0/NX * fmax(-0.5,signdisthor);
      //if(i==0 && j>12 && j<20 && k>12 && k<20)
       //  cout<<"\n init(i,j,k)="<<j<<","<<k<<","<<init(i,j,k)<<"\n";
     // if(i<=Nx)init(i,j,k) = 1.0/Nx * (sqrt(disthor*disthor+(i-Nx)*(i-Nx)))-1.0E-5 ; 
          //else init(i,j,k) = 1.0/Nx * fmax(-1.0,signdisthor);
   }
      //init(i,j,k) = 1.0/NX *(sqrt((i-16) * (i-16) + 16*j*j + (k-16) * (k-16))-10);
   END_FOR_THREE
   /*for(int i=8;i<=25;i++)
      cout<<"5555555555555555555555555"<<i<<","<<init(i,0,16)<<","<<init(i,1,16)<<"\n";*/
}


